package com.ketai.web;

import cn.hutool.core.date.DateTime;
import cn.hutool.db.Page;
import com.alibaba.fastjson.JSON;
import com.ketai.common.MessageConstant;
import com.ketai.common.Result;
import com.ketai.common.StatusCode;
import com.ketai.pojo.Organization;
import com.ketai.service.OrganizationService;
import com.ketai.service.impl.OrganizationServiceImpl;
import com.ketai.util.DateTimeUtil;
import com.ketai.util.Pages;
import com.ketai.util.SendUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: 高伟华
 * @Description:
 * @Date:Create In 2022-08-06 15:33
 **/
@WebServlet("/admin/organization")
public class OrganizationServlet extends HttpServlet {

    OrganizationService organizationService =new OrganizationServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String opr = req.getParameter("opr");
        if ("list".equals(opr)) {
            List<Organization> organizationList = organizationService.queryOrganizationList();

            Pages<Organization>  page= new Pages<Organization>();

            page.setDataItems(organizationList);
            page.setDataTotal(5L);


            SendUtil.SendPageJson(resp, page);
        } else if ("delete".equals(opr)) {
            Integer id = Integer.parseInt(req.getParameter("id"));
            int result = organizationService.deleteOrganizationById(id);
            Result r = Result.message(MessageConstant.MECHANISM_DELETE_SUCCESS);
            if (result == 0) {
                r = Result.error(StatusCode.SQLERROR, MessageConstant.MECHANISM_DELETE_ERROR);
            }
        }else if("save".equals(opr)){
            String name = req.getParameter("name");
            String brief  =  req.getParameter("brief");
            String respon  =  req.getParameter("Respon");
            String number =   req.getParameter("number");
            String address  = req.getParameter("address");
            String created =  req.getParameter("Created");
            String images  = req.getParameter("images");
            Organization organization = new Organization(name,brief,respon,number,address,created,images);
            organizationService.addOrganization(organization);
            SendUtil.Sendjson(resp, Result.ok(MessageConstant.MECHANISM_SAVE_SUCCESS, 100));
        }else if ("byid".equals(opr)){
            String id =req.getParameter("id");
            Organization organization=organizationService.organizationById(id);
            SendUtil.SendjsonFormart(resp, organization);
        }else if("check".equals(opr)){
            String id  = req.getParameter("id");
            String open = req.getParameter("open");
            String desc =  req.getParameter("desc");
            String pid = "1";
            int result  = organizationService.checkedorganization(id,open,desc,pid);
            if(result>0){
                SendUtil.Sendjson(resp,MessageConstant.ACTITY_CHECK_SUCCESS);
            }else{
                SendUtil.Sendjson(resp,MessageConstant.ACTITY_CHECK_ERROR);
            }
        }
        }

}
